import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Inject } from '@nestjs/common';
import { PositionsService } from './positions.service';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { SetLeverageDto } from './dto/setLeverage.dto';
import { Cron, CronExpression, Interval } from '@nestjs/schedule';
import { OrderService } from '../order/order.service';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';

const dayjs = require('dayjs');

@ApiBearerAuth()
@ApiTags('仓位')
@Controller('positions')
export class PositionsController {
  constructor(
    private readonly positionsService: PositionsService,
    private readonly orderService: OrderService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {}

  // 定时任务，每天凌晨1点执行
  @Cron('0 0 1 * * *')
  timeTask() {
    const date = dayjs().subtract(1, 'day').format('YYYY-MM-DD 24:00:00');
    this.startTongbu(date);
  }

  // 定时任务2，每天18点同步
  // @Cron('0 40 18 * * *')
  // timeTask2() {
  //   const date = dayjs().format('YYYY-MM-DD 18:30:00');
  //   this.startTongbu(date);
  // }

  // 测试定时 ================================================================
  // @Cron('30 17 11 * * *')
  // async timeTask3() {
  //   const date = dayjs().format('2025-04-11 24:00:00');
  //   await this.orderService.tongBuWeituo({ date }); // 历史委托
  //   await this.orderService.tongBuChengJiao({ date }); // 历史成交
  // }

  // 处理爆仓-定时
  @Interval(2000)
  handleInterval() {
    const date = dayjs().format('YYYY-MM-DD HH:mm:ss');
    const APP_ENV = process.env.APP_ENV;
    if (APP_ENV !== 'development') this.positionsService.autoBaoCang();
  }

  // 统一处理同步任务
  async startTongbu(date) {
    console.log('====定时任务开始执行');
    this.logger.info(`${date},定时任务开始执行，开始同步数据`);
    console.log('当前时间day', dayjs().format('YYYY-MM-DD HH:mm:ss'));
    this.positionsService.tongBuHistoryPositions({ date }); // 历史仓位
    this.logger.info(`控制器: 历史仓位同步结束`);
    this.orderService.tongBuWeituo({ date }); // 历史委托
    this.logger.info(`控制器: 历史委托同步结束`);
    this.orderService.tongBuChengJiao({ date }); // 历史成交
    this.logger.info(`控制器: 历史成交同步结束`);
  }
  async startTongbu_Old(date) {
    console.log('====定时任务开始执行');
    this.logger.info(`${date},定时任务开始执行，开始同步数据`);
    console.log('当前时间day', dayjs().format('YYYY-MM-DD HH:mm:ss'));
    await this.positionsService.tongBuHistoryPositions({ date }); // 历史仓位
    this.logger.info(`控制器: 历史仓位同步结束`);
    await this.orderService.tongBuWeituo({ date }); // 历史委托
    this.logger.info(`控制器: 历史委托同步结束`);
    await this.orderService.tongBuChengJiao({ date }); // 历史成交
    this.logger.info(`控制器: 历史成交同步结束`);
  }

  @Post('setLeverage')
  @ApiOperation({
    summary: '设置杠杆',
    description: `1.逐仓: leverage:倍数  2. 全仓：leverage: 0,cross_leverage_limit: 倍数`,
  })
  create(@Body() SetLeverageDto: SetLeverageDto) {
    return this.positionsService.setLeverage(SetLeverageDto);
  }

  @Get('positions')
  @ApiOperation({ summary: '仓位列表', description: '仓位列表' })
  positions(@Query() params: any) {
    return this.positionsService.positions(params);
  }

  // 历史仓位
  @Get('historyPositions')
  @ApiOperation({ summary: '历史仓位', description: '历史仓位' })
  async historyPositions(@Query() params: any) {
    return this.positionsService.historyPositions(params);
  }

  // 历史仓位-黑洞
  @Get('myHistoryPosition')
  @ApiOperation({ summary: '历史仓位-黑洞', description: '历史仓位-黑洞' })
  async myHistoryPosition(@Query() params: any) {
    return this.positionsService.myHistoryPosition(params);
  }

  // 止盈止损
  @Post('zhiYingSun')
  @ApiOperation({ summary: '止盈止损', description: `止盈止损` })
  zhiYingSun(@Body() params: any) {
    return this.positionsService.zhiYingSun(params);
  }

  // 获取止盈止损列表
  @Get('getZhiYingSun')
  @ApiOperation({ summary: '获取止盈止损列表', description: `获取止盈止损列表` })
  getZhiYingSun(@Query() params: any) {
    return this.positionsService.getZhiYingSun(params);
  }

  // 删除止盈止损
  @Get('deleteZhiYingSun')
  @ApiOperation({ summary: '删除止盈止损', description: `删除止盈止损` })
  deleteZhiYingSun(@Query() params: any) {
    return this.positionsService.deleteZhiYingSun(params);
  }

  // 重置用户的芝麻盈亏、同步时间  async resetUserYingKui() {
  @Get('resetUserYingKui')
  @ApiOperation({ summary: '重置用户的芝麻盈亏、同步时间' })
  resetUserYingKui(@Query() params: any) {
    return this.positionsService.resetUserYingKui(params);
  }

  @Get('htPositions')
  @ApiOperation({ summary: '后台仓位列表', description: '后台仓位列表' })
  htPositions(@Query() params: any) {
    return this.positionsService.htPositions(params);
  }

  @Get('htLSPositions')
  @ApiOperation({ summary: '后台历史仓位列表', description: '后台历史仓位列表' })
  htLSPositions(@Query() params: any) {
    return this.positionsService.htLSPositions(params);
  }

  @Get('tongBuHistoryPositions')
  @ApiOperation({ summary: '定时同步芝麻的历史仓位', description: '定时同步芝麻的历史仓位' })
  tongBuHistoryPositions(@Query() params: any) {
    return this.positionsService.tongBuHistoryPositions(params);
  }

  // 特殊使用
  @Get('delAllLiShiPosition')
  @ApiOperation({ summary: '删除所有历史仓位', description: '删除所有历史仓位' })
  delAllLiShiPosition(@Query() params: any) {
    return this.positionsService.delAllLiShiPosition();
  }

  // 删除指定时间段的历史仓位
  @Get('delLiShiPosition')
  @ApiOperation({ summary: '删除指定时间段的历史仓位' })
  delLiShiPosition(@Query() params: any) {
    return this.positionsService.delLiShiPosition(params);
  }

  // 重置同步信息: 删除所有的记录,历史仓位,用户同步记录
  @Get('resetSyncData')
  @ApiOperation({ summary: '重置同步信息' })
  resetSyncData(@Query() params: any) {
    return this.positionsService.resetSyncData();
  }

  // 自动爆仓
  @Get('autoBaoCang')
  @ApiOperation({ summary: '自动爆仓' })
  autoBaoCang(@Query() params: any) {
    return this.positionsService.autoBaoCang();
  }

  // 一键平仓
  @Get('yiJianPingCang')
  @ApiOperation({ summary: '一键平仓' })
  yiJianPingCang(@Query() params: any) {
    return this.positionsService.yiJianPingCang();
  }
}
